//
//  RSEmotionKeyboardView.swift
//  RSWeibo
//
//  Created by Macx on 16/8/20.
//  Copyright © 2016年 Macx. All rights reserved.
//

import UIKit

// 重用标记
let emotionCellWithReuseIdentifier = "emotionCellWithReuseIdentifier"

class RSEmotionKeyboardView: UIView {
    
    // 底部的工具栏按钮
    private lazy var emotionToolbar: RSEmotionToolBarView = {
        // UIStackView不能自己调用frame方法, 只能自己手动调用
        let tool = RSEmotionToolBarView(frame: CGRectZero)
        
            return tool
        
        }()
    
    
    // 一个显示键盘的collectionView
    private lazy var emotionKeyboardView: UICollectionView = {
        
        // 创建collectionView必须要有一个 flowlayout
        let flowlayout = UICollectionViewFlowLayout()
        
        // 滚动方向: 水平方向
        flowlayout.scrollDirection = .Horizontal
        
        
    
        let ek = UICollectionView(frame: CGRectZero, collectionViewLayout: flowlayout)
        
        // 背景颜色
        ek.backgroundColor = UIColor.blueColor()
        // 分页
        ek.pagingEnabled = true
        // 弹簧
        ek.bounces = false

        
        // 设置数据源代理
        ek.dataSource = self
        
        // 注册cell
        ek.registerClass(UICollectionViewCell.self, forCellWithReuseIdentifier: emotionCellWithReuseIdentifier)
        
        
        return ek
    }()
    
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        
        setupUI()
        
    }
    
    required init?(coder aDecoder: NSCoder) {
     super.init(coder: aDecoder)
    }
    

    private func setupUI(){
        

        // 添加子控件
        addSubview(emotionToolbar)
        
        // 添加表情键盘的cell
        addSubview(emotionKeyboardView)
        
        // 设置约束
        emotionToolbar.snp_makeConstraints { (make) in
            make.leading.equalTo(self)
            make.trailing.equalTo(self)
            make.bottom.equalTo(self)
            make.height.equalTo(35)
        }
        
        emotionKeyboardView.snp_makeConstraints { (make) in
            make.leading.equalTo(self)
            make.trailing.equalTo(self)
            make.top.equalTo(self)
            make.bottom.equalTo(emotionToolbar.snp_top)
        }
        
        // 定义点击按钮的闭包, 跳转到这里执行
        emotionToolbar.didselectedEmotionToolBarBtnClosure = { (type: RSEmotionButtonType)->() in
            
            // 判断不同的按钮, 执行不同的点击操作
            switch type {
                
                case .Normal:
                    print("Normal")
                case .Emoji:
                    print("Emoji")
                case .LXH:
                    print("LXH")
            }
        
            
        }
    }
    
    // 重写子控件的布局方法
    override func layoutSubviews() {
        super.layoutSubviews()
        
        // 取出emotionKeyboardView的 flowLayout
        let flowLayout = emotionKeyboardView.collectionViewLayout as! UICollectionViewFlowLayout
        // 设置flowLayout的参数
        // 大小
        flowLayout.itemSize = emotionKeyboardView.frame.size
        // 水平间距
        flowLayout.minimumInteritemSpacing = 5
        // 垂直间距
        flowLayout.minimumLineSpacing = 10
    }
    
}

// 键盘的 collectionView 的数据源代理方法
extension RSEmotionKeyboardView: UICollectionViewDataSource {
    
    func numberOfSectionsInCollectionView(collectionView: UICollectionView) -> Int {
        return 3
    }
    
    func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return 2
    }
    
    func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
        let cell = collectionView.dequeueReusableCellWithReuseIdentifier(emotionCellWithReuseIdentifier, forIndexPath: indexPath)
        
        cell.contentView.backgroundColor = UIColor.greenColor()
        
        return cell
    }

    
}
























